package com.dell.optimize;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsActivity extends Activity{

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		final EditText timeInterval = (EditText)findViewById(R.id.interval_val);
		final EditText fileSize = (EditText)findViewById(R.id.size_val);
		
		final Button saveBtn = (Button)findViewById(R.id.save_btn);
		
		saveBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int interval = 0;
				try{
					interval = Integer.parseInt(timeInterval.getText().toString());
				}catch (Exception e) {
					// TODO: handle exception
				}
				
				int size = 0;
				try{
					size = Integer.parseInt(fileSize.getText().toString());
				}catch (Exception e) {
					// TODO: handle exception
				}
				System.out.println(" Val "+size);
				System.out.println(" Val "+interval);
				if(interval == 0 || size == 0){
					Toast.makeText(saveBtn.getContext(), "Enter valid value to save", Toast.LENGTH_LONG).show();
					return;
				}
				System.out.println(" Data Entered");
				SharedPreferences settings = getSharedPreferences(Util.SETTINGS, 0);
				
				SharedPreferences.Editor editor = settings.edit();
				editor.putInt(Util.INTERVAL, interval);
				editor.putInt(Util.SIZE, size);
				
				editor.commit();
				Toast.makeText(saveBtn.getContext(), "Data Saved", Toast.LENGTH_LONG).show();
			}
			
		});
		
		SharedPreferences settings = getSharedPreferences(Util.SETTINGS, 0);
		String msg = settings.getString(Util.ERR_MSG, "");
		final TextView infos = (TextView)findViewById(R.id.info_err);
		if(msg.length() > 0){
			infos.setText(msg);
		}
		
		Button clearBtn = (Button)findViewById(R.id.clear_btn);
		clearBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				SharedPreferences settings = getSharedPreferences(Util.SETTINGS, 0);
				
				SharedPreferences.Editor editor = settings.edit();
				editor.putString(Util.ERR_MSG, "Info-Err : ");
				editor.commit();
				infos.setText("msg cleared");
				((LinearLayout)findViewById(R.id.settings_layout)).invalidate();
				
			}
			
		});
		    
	}
}
